package org.emergency.framework.serviceImpl.oa;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.emergency.framework.common.IdsService;
import org.emergency.framework.constant.IDConstant;
import org.emergency.framework.dao.IUtilDao;
import org.emergency.framework.dao.oa.ITbPermissionDAO;
import org.emergency.framework.model.TbPermission;
import org.emergency.framework.model.TbRole;
import org.emergency.framework.model.TbRolePermission;
import org.emergency.framework.service.common.IRolePermissionMapping;
import org.emergency.framework.service.common.ITbPermissionTreeService;
import org.emergency.framework.service.oa.ITbPermissonService;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;

@Service("tbpermissionServiceImpl")
public class TbPermissonServiceImpl implements ITbPermissonService {
	

	@Resource(name = "rolePermissionMappingImpl")
	private IRolePermissionMapping rolePermissionMapping;
	
	@Resource(name="tbPermissionTreeServiceImpl")
	private ITbPermissionTreeService iTbPermissionTreeService;

	@Resource(name = "tbPermissionDAO")
	private ITbPermissionDAO tbPermissionDAO;

	@Resource(name = "idsServiceImpl")
	private IdsService idsService;

	public List<TbPermission> findAll() {
		String hqlString = "from TbPermission t where t.isEnabled = true";
		return tbPermissionDAO.getHibernateTemplate().find(hqlString);
	}

	public List<TbPermission> queryPermissinByRoleId(String roleId) {
		String hqlString = "select t.tbPermission from TbRolePermission t where t.tbRole.tbRoleId="
				+ roleId + " " + " and t.tbPermission.isEnabled = true";
		List li = tbPermissionDAO.getHibernateTemplate().find(hqlString);
		return li;
	}

	// public void saveRolePmsnByRoleId(TbRole tbRole, String rolePmsnStr,
	// ServletContext servletContext) {
	// String hqlString = "delete TbRolePermission t where t.tbRole.tbRoleId='"
	// + tbRole.getTbRoleId() + "'";
	//
	// tbPermissionDAO.executeDelOrUpdateHql(hqlString);// 把那些删掉
	// String[] permissionArray = rolePmsnStr.split(",");
	// List<String> permissionList = Arrays.asList(permissionArray);
	//
	// /*
	// * 以下为批量增加
	// */
	// for (String s : permissionList) {
	// String tbRolePermissionId = idsService
	// .findTablePK(IDConstant.TB_ROLE_PERMISION);
	//
	// TbRolePermission tbRolePermission = new TbRolePermission();
	// tbRolePermission.setTbRolePermissionId(tbRolePermissionId);
	//
	// TbRole tbRole2 = new TbRole();
	// tbRole2.setTbRoleId(tbRole.getTbRoleId());
	//
	// TbPermission tbPermission = new TbPermission();
	// tbPermission.setPId(s);
	//
	// tbRolePermission.setTbPermission(tbPermission);
	// tbRolePermission.setTbRole(tbRole2);
	// tbPermissionDAO.getHibernateTemplate().save(tbRolePermission);
	// }
	//
	// }

	@Override
	public void submitRolePermissionList(String roleId,
			List<String> permissionIdStringList) {
		// TODO Auto-generated method stub
		Session session = tbPermissionDAO.getCSession();
		Transaction tx = null;
		try {

			tx = session.beginTransaction(); // 开启事务
			String hqlString = "delete TbRolePermission t where t.tbRole.tbRoleId = '"
					+ roleId + "'";

			Query query = session.createQuery(hqlString);
			query.executeUpdate(); // 删除中间表中的内容
			/*
			 * 以下为批量增加
			 */
			for (String s : permissionIdStringList) {
				String tbRolePermissionId = idsService
						.findTablePK(IDConstant.TB_ROLE_PERMISION);

				TbRolePermission tbRolePermission = new TbRolePermission();
				TbRole tbRole = new TbRole();
				tbRole.setTbRoleId(roleId);
				TbPermission tbPermission = new TbPermission();
				tbPermission.setPId(s);
				tbRolePermission.setTbRolePermissionId(tbRolePermissionId);
				tbRolePermission.setTbPermission(tbPermission);
				tbRolePermission.setTbRole(tbRole);
				session.saveOrUpdate(tbRolePermission);
			}
			tx.commit();// 提交事务
			rolePermissionMapping.updateMap();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx.isActive()) {
				try {
					tx.rollback();
				} catch (Exception ex) {
					// TODO: handle exception
					ex.printStackTrace();
				}

			}
		}

	}

	@Override
	public boolean delete(TbPermission entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public TbPermission loadObjectById(Serializable id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean save(TbPermission entity) {
		// TODO Auto-generated method stub
		/*
		 * 加id
		 */
		entity.setPId(idsService.findTablePK(IDConstant.TB_PERMISSION));
		entity.setEnabled(true);
		try {
			
			tbPermissionDAO.save(entity);
			iTbPermissionTreeService.reloadTree();//更改了数据库，要修改内存树
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean saveOrUpdate(TbPermission entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(TbPermission entity) {
		// TODO Auto-generated method stub
		TbPermission tbPermission = tbPermissionDAO.loadObjectById(entity.getPId());
		tbPermission.setPName(entity.getPName());
		tbPermission.setDescription(entity.getDescription());
		try {
			tbPermissionDAO.update(tbPermission);
			iTbPermissionTreeService.reloadTree();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
